package com.sunshine.wrongquestionset.room.dao

import androidx.lifecycle.LiveData
import androidx.room.*
import com.sunshine.wrongquestionset.room.entity.QuestionSetActivityBean
import com.sunshine.wrongquestionset.room.entity.QuestionSetEntity

/**
 * @author sunshine
 * @date 2021/3/28
 */
@Dao
interface QuestionSetDao {
    @Query("INSERT INTO question_set(userId, name, description, subjectId, backgroundColor) VALUES(:userId, :name, :description, :subjectId, :backgroundColor)")
    fun insert(userId: String, name: String, description: String?, subjectId: Int, backgroundColor: String)

    @Insert(entity = QuestionSetEntity::class, onConflict = OnConflictStrategy.IGNORE)
    fun insertByEntity(questionSetEntity: QuestionSetEntity)

    @Delete(entity = QuestionSetEntity::class)
    fun delete(questionSetEntity: QuestionSetEntity)

    @Query("SELECT * FROM question_set WHERE userId=:userId")
    fun selectByUserId(userId: String): LiveData<List<QuestionSetEntity>?>

    @Query("SELECT * FROM question_set WHERE userId=:userId and subjectId=:subjectId")
    fun selectBySubjectId(userId: String, subjectId: Int): LiveData<List<QuestionSetEntity>?>

    @Query("SELECT id FROM question_set WHERE userId=:userId and subjectId=:subjectId")
    fun selectIdBySubjectId(userId: String, subjectId: Int): LiveData<List<Int>?>

    //查询某科目中错题本的个数
    @Query("SELECT COUNT(*) FROM question_set WHERE subjectId=:subjectId")
    fun selectCountInSubjectId(subjectId: Int): LiveData<Int>

    @Query("SELECT question_set.id, question_set.userId, question_set.name, question_set.description, question_set.subjectId, subject.name as subjectName, question_set.backgroundColor, question_set.id as questionSetId FROM question_set,subject WHERE question_set.subjectId = subject.id and question_set.userId=:userId")
    fun selectQuestionSetActivityBeanList(userId: String): LiveData<List<QuestionSetActivityBean>?>

    @Query("SELECT question_set.id, question_set.userId, question_set.name, question_set.description, question_set.subjectId, subject.name as subjectName, question_set.backgroundColor, question_set.id as questionSetId FROM question_set,subject WHERE question_set.subjectId = subject.id and question_set.userId=:userId and question_set.subjectId=:subjectId")
    fun selectQuestionSetActivityBeanListBySubjectId(userId: String, subjectId: Int): LiveData<List<QuestionSetActivityBean>?>

    @Update(entity = QuestionSetEntity::class)
    fun update(questionSetEntity: QuestionSetEntity)
}